package com.heima.wemedia.crawler;

import com.alibaba.fastjson.JSON;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.WemediaApplication;
import com.heima.wemedia.service.WmNewsService;
import org.apache.avro.data.Json;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@SpringBootTest(classes = WemediaApplication.class)
@RunWith(SpringRunner.class)
public class NewArticles {
    @Autowired
    WmNewsService wmNewsService;

    @Test
    public void getNewArticles() {
        String url = "https://www.zhihu.com/rss"; // 替换为实际的知乎RSS链接
        try {
            // 连接到URL并获取HTML文档
            Document doc = Jsoup.connect(url).get();
            // 获取所有文章项
            Elements items = doc.select("item");
            for (Element item : items) {
                // 获取标题
                String title = item.select("title").text();
                // 获取正文内容并去除HTML标记
                String description = item.select("description").text();
                String cleanDescription = removeHtmlTags(description);
                // 获取图片内容（假设图片在description中）
                String imageUrl = item.select("description img").attr("src");
                // 获取发布时间并格式化
                String pubDate = item.select("pubDate").text();
                Date formattedDate;
                if (pubDate==null){
                     formattedDate = formatPubDate(pubDate);
                }else {
                     formattedDate = new Date();
                }

                if (title.length()>36){
                    title=title.substring(0,36);
                }
                WmNewsDto wmNewsDto = new WmNewsDto();
                wmNewsDto.setTitle(title);
                List<Map<String,String>> list=new ArrayList<>();
                Map<String,String> map=new HashMap<>();
                map.put("type","text");
                map.put("value",cleanDescription);
                list.add(map);
                String content = JSON.toJSONString(list);
                wmNewsDto.setContent(content);
                wmNewsDto.setPublishTime(new Date());
                wmNewsDto.setType((short) 0);
                wmNewsDto.setLabels("知乎文章");
                wmNewsDto.setChannelId(1);
                wmNewsDto.setStatus((short) 8);
                wmNewsService.submitNews(wmNewsDto);
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }

    // 去除HTML标记的正则表达式方法
    private static String removeHtmlTags(String html) {
        return html.replaceAll("<[^>]*>", "");
    }

    // 格式化发布时间的方法
    private static Date formatPubDate(String pubDate) throws ParseException {
        SimpleDateFormat originalFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
        SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        return originalFormat.parse(pubDate);

    }
}



